import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'store/index.dart';

class RProviderPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('RProvider Example')),
      body: ProviderScope(child: _selectChild()),
    );
  }
}

class NotifyApp extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final appState = ref.watch(appStateProvider);

    return Scaffold(
      body: Center(
        child: Column(
          children: [
            Text('Name: ${appState.name}'),
            Text('Age: ${appState.age}'),
            Text('Premium: ${appState.isPremium}'),
            ElevatedButton(
              onPressed: () => ref.read(appStateProvider.notifier).updateName('Alice'),
              child: Text('Update Name'),
            ),
            ElevatedButton(
              onPressed: () => ref.read(appStateProvider.notifier).incrementAge(),
              child: Text('Increment Age'),
            ),
            ElevatedButton(
              onPressed: () => ref.read(appStateProvider.notifier).togglePremium(),
              child: Text('Toggle Premium'),
            ),
          ],
        ),
      ),
    );
  }
}

class CounterHomePage extends ConsumerWidget {
  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final counter = ref.watch(counterNotifierProvider); // 监听状态
    return Scaffold(
      appBar: AppBar(title: Text("NotifierProvider 示例")),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text("点击次数：$counter"),
            ElevatedButton(
              onPressed: () =>
                  ref.read(counterNotifierProvider.notifier).increment(),
              child: Text("增加计数"),
            ),
          ],
        ),
      ),
    );
  }
}

class _selectChild extends ConsumerWidget {
  const _selectChild({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // 使用 select 只监听 name 字段的变化
    // print(31);
    final name = ref.watch(userProvider.select((user) => user.name));
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text(' $name'),
        ElevatedButton(
          onPressed: () {
            ref
                .read(userProvider.notifier)
                .update((user) => user.copyWith(name: 'New Name'));
          },
          child: const Text('Increment Counter'),
        ),
        ElevatedButton(
          onPressed: () {
            // ref.read(userProvider.notifier).state.name = 'New Name';
          },
          child: const Text('Change Name'),
        ),
      ],
    );
  }
}

class _pchildren extends ConsumerWidget {
  const _pchildren({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final state = ref.watch(combinedProvider);
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text('${state.counter}, ${state.name}'),
        ElevatedButton(
          onPressed: () {
            ref.read(counterProvider.notifier).state++;
          },
          child: const Text('Increment Counter'),
        ),
        ElevatedButton(
          onPressed: () {
            ref.read(nameProvider.notifier).state = 'New Name';
          },
          child: const Text('Change Name'),
        ),
      ],
    );
  }
}
